package com.letoken.platform.pub.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * DateUtil
 */
public class DateUtil {


    public enum TIME {
        TIME_BEGIN("000000"), TIME_END("235959");
        private String value;

        //构造方法
        TIME(String value) {
            this.setValue(value);
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public enum Format {
        DATATIME_UNSIGNED("yyyyMMddHHmmss");
        private String value;

        //构造方法
        Format(String value) {
            this.setValue(value);
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }


    public static String format(Date date, String pattern) {
        DateFormat df = createDateFormat(pattern);
        return df.format(date);
    }

    private static DateFormat createDateFormat(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        TimeZone gmt = TimeZone.getTimeZone("GMT");
        sdf.setTimeZone(gmt);
        sdf.setLenient(true);
        return sdf;
    }

    public static Date getNowDateTime() {
        return new Date();
    }

    public static String getToDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(calendar.getTime());
    }

    public static String getYestDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(calendar.getTime());
    }

    /**
     * 获取指定时间后7天日期
     *
     * @return
     */
    public static Date getSevenDay(Date startTime) {
        Date endTime = new Date(startTime.getTime() + 168 * 60 * 60 * 1000);
        return endTime;
    }

    public static Date format(String date, TIME time, Format format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format.getValue());
            return sdf.parse(date + time.getValue());
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 获取前/后半年的结束时间
     *
     * @return
     */
    public static Date getEffectiveTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, +6);
        return calendar.getTime();
    }


    /**
     * 获取昨天五点钟时间
     *
     * @return
     */
    public static LocalDateTime getYesterdayStartTime(LocalDateTime nowDate) {
        int hour = nowDate.getHour();
        LocalDateTime resDate = null;
        if (hour < 5) {
            resDate = nowDate.withDayOfMonth(nowDate.getDayOfMonth() - 1).withHour(5).withMinute(0).withSecond(0);
        } else {
            resDate = nowDate.withHour(5).withMinute(0).withSecond(0);
        }
        return resDate;
    }

    /**
     * 获取结算五点钟时间
     *
     * @return
     */
    public static LocalDateTime getSettlementTime(LocalDateTime nowDate) {
        int hour = nowDate.getHour();
        LocalDateTime resDate = null;
        if (hour < 5) {
            resDate = nowDate.withHour(5).withMinute(0).withSecond(0);
        } else {
            resDate = nowDate.withDayOfMonth(nowDate.getDayOfMonth() + 1).withHour(5).withMinute(0).withSecond(0);
        }
        return resDate;
    }

    public static String getToDayFiveHour(LocalDateTime nowDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        int hour = nowDate.getHour();
        if (hour - 5 < 0) {
            nowDate = nowDate.withDayOfMonth(nowDate.getDayOfMonth() - 1);
        }
        return formatter.format(nowDate);
    }

    public static String getYesterdayFiveHour(LocalDateTime nowDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime date = nowDate.withDayOfMonth(nowDate.getDayOfMonth() - 1);
        return formatter.format(date);
    }


    /**
     * 传入北京Data类型日期，返回字符串类型时间（ISO8601标准时间）
     *
     * @param date
     * @return
     */
    public static String getISO8601Timestamp(Date date) {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        dft.setTimeZone(tz);
        String isoDate = dft.format(date);
        return isoDate;
    }

    /**
     * 获取当前时间前一个小时时间字符串，返回字符串类型时间（ISO8601标准时间）
     *
     * @return //
     */
    public static String getTimestamp() {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        dft.setTimeZone(tz);
        return dft.format(calendar.getTime());
    }

    /**
     * 11位的时间戳
     * @return
     */
    public static String getUnixTimestamp() {
        return Long.toString(System.currentTimeMillis()/1000);
    }

    /**
     * 13位的时间戳
     * @return
     */
    public static String getUnixTimestamp13() {
        return Long.toString(System.currentTimeMillis());
    }

    public static void main(String[] args) {
        System.out.println(getISO8601Timestamp(new Date()));
        System.out.println(getTimestamp());
    }


}

